﻿public namespace System
{	
	public class Timer
	{
		private num id = 0;
		private num delay = 0;
		private bool isInterval = false;
		
		public ctor( num delay )
		{
			this.delay = delay;
		}

		public ctor( num delay, bool isInterval )
		{
			this.delay = delay;
			this.isInterval = isInterval;
		}
		
		public void Start()
		{
			this.Stop();
			if ( this.isInterval )
			{
				this.id = StartIntervalTimer( this.OnTimeout, this.delay );
			}
			else
			{
				this.id = StartTimer( this.OnTimeout, this.delay );
			}
		}
		
		public void Stop()
		{
			if ( this.id != 0 )
			{
				if ( this.isInterval )
				{
					StopIntervalTimer( this.id );
				}
				else
				{
					StopTimer( this.id );
				}
				this.id = 0;
			}
		}
		
		public event Timer Timeout;
		
		private void OnTimeout()
		{
			this.Timeout(this);
		}
	}
	
	@Native("$System.StartTimer")
	public num StartTimer( void->void func, num delay);
	@Native("$System.StopTimer")
	public void StopTimer( num timerID );
	@Native("$System.StartIntervalTimer")
	public num StartIntervalTimer( void->void func, num interval);
	@Native("$System.StopIntervalTimer")
	public void StopIntervalTimer( num timerID );
}
